package com.aatattoo.light_food_service.service;

import com.apistd.uni.Uni;
import com.apistd.uni.sms.UniMessage;
import com.apistd.uni.sms.UniSMS;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @Author aatattoo
 * @Date 2021/12/14 10:32 上午
 * @msg:
 */
@Service
@Component
public class SmsService {
    private  String ACCESS_KEY_ID = "Qsghj6wCrXk56HosvdFN597mfyfQgvRpThcTKAZ5Pt3r9er81";
    private  String ACCESS_KEY_SECRET = "by2AeSzSsELjwsJyx7nHBGJaxTG24";


    public UniMessage sendSMS(String phoneNumber,String type,String msgCode) {
        // 初始化
        Uni.init(ACCESS_KEY_ID); // 若使用简易验签模式仅传入第一个参数即可
        // 设置自定义参数 (变量短信)
        Map<String, String> templateData = new HashMap<String, String>();
        templateData.put("code", msgCode);
        templateData.put("ttl","15");
        // 构建信息
        UniMessage message = UniSMS.buildMessage()
                .setTo(phoneNumber)
                .setSignature("aatattoo")
                .setTemplateId(type)
                .setTemplateData(templateData);
        return message;
    }

    /**
     * 随机生成验证码
     *
     * @return
     */
    public String getMsgCode() {
        int n = 6;
        StringBuilder code = new StringBuilder();
        Random ran = new Random();
        for (int i = 0; i < n; i++) {
            code.append(Integer.valueOf(ran.nextInt(10)).toString());
        }
        return code.toString();
    }



}